
#ifndef __ASF_INTF_H__
#define __ASF_INTF_H__


#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* End of #ifdef __cplusplus */


#define FORMAT_NAME_LEN			32
#define MAX_FILE_NAME			64
#define CODEC_INFO_LEN			64
#define ASF_MAX_STREAMS		    20   /*max asf stream number*/

#ifdef _WIN32
typedef unsigned __int64 ASF_U_64;
typedef __int64  ASF_64;
#else
typedef unsigned long long ASF_U_64;
typedef long long ASF_64;		
#endif

typedef enum hiASF_PIX_FMT_E
{
    ASF_PIX_FMT_NONE= -1,
    ASF_PIX_FMT_YUV420P,   ///< Planar YUV 4:2:0 (1 Cr & Cb sample per 2x2 Y samples)
    ASF_PIX_FMT_YUV422,    ///< Packed pixel, Y0 Cb Y1 Cr
    ASF_PIX_FMT_RGB24,     ///< Packed pixel, 3 bytes per pixel, RGBRGB...
    ASF_PIX_FMT_BGR24,     ///< Packed pixel, 3 bytes per pixel, BGRBGR...
    ASF_PIX_FMT_YUV422P,   ///< Planar YUV 4:2:2 (1 Cr & Cb sample per 2x1 Y samples)
    ASF_PIX_FMT_YUV444P,   ///< Planar YUV 4:4:4 (1 Cr & Cb sample per 1x1 Y samples)
    ASF_PIX_FMT_RGBA32,    ///< Packed pixel, 4 bytes per pixel, BGRABGRA..., stored in cpu endianness
    ASF_PIX_FMT_YUV410P,   ///< Planar YUV 4:1:0 (1 Cr & Cb sample per 4x4 Y samples)
    ASF_PIX_FMT_YUV411P,   ///< Planar YUV 4:1:1 (1 Cr & Cb sample per 4x1 Y samples)
    ASF_PIX_FMT_RGB565,    ///< always stored in cpu endianness
    ASF_PIX_FMT_RGB555,    ///< always stored in cpu endianness, most significant bit to 1
    ASF_PIX_FMT_GRAY8,
    ASF_PIX_FMT_MONOWHITE, ///< 0 is white
    ASF_PIX_FMT_MONOBLACK, ///< 0 is black
    ASF_PIX_FMT_PAL8,      ///< 8 bit with RGBA palette
    ASF_PIX_FMT_YUVJ420P,  ///< Planar YUV 4:2:0 full scale (jpeg)
    ASF_PIX_FMT_YUVJ422P,  ///< Planar YUV 4:2:2 full scale (jpeg)
    ASF_PIX_FMT_YUVJ444P,  ///< Planar YUV 4:4:4 full scale (jpeg)
    ASF_PIX_FMT_XVMC_MPEG2_MC,///< XVideo Motion Acceleration via common packet passing(xvmc_render.h)
    ASF_PIX_FMT_XVMC_MPEG2_IDCT,
    ASF_PIX_FMT_UYVY422,   ///< Packed pixel, Cb Y0 Cr Y1
    ASF_PIX_FMT_UYVY411,   ///< Packed pixel, Cb Y0 Y1 Cr Y2 Y3
    ASF_PIX_FMT_NB    
} ASF_PIX_FMT_E;

#define ERR_ASF_REGISTER_AV         0x80000011
#define ERR_ASF_OPEN_FILE           0x80000012
#define ERR_ASF_ENDFILE             0x80000013
#define ERR_ASF_READ_FRAME          0x80000014

#define ERR_ASF_OK                  0
#define ERR_ASF_INVALIDARG          0x80000001
#define ERR_ASF_INVALIDSTATE        0x80000002
#define ERR_ASF_MALLOC              0x80000003
#define ERR_ASF_UNSUPPORT_CODEC     0x80000004
#define ERR_ASF_OPERATIONfAILED     0x80000005

//#define ERR_ASF_REGISTER_AV 1
//#define ERR_ASF_OPEN_FILE 2
//#define ERR_ASF_ENDFILE 3
//#define ERR_ASF_READ_FRAME 4

#define ERR_ASF_INVALIDARG     0x80000001
#define ERR_ASF_INVALIDSTATE   0x80000002
#define ERR_ASF_MALLOC         0x80000003
#define ERR_ASF_UNSUPPORT_CODEC     0x80000004


typedef void*     ASFHandle ;

typedef time_t (*AsfDateTimeCallBack)();

/************************************************
 * MP4支持的音视频码流格式
 *
 * ASF_CODEC_ID_H264:	  	H264视频编码    
 *
 * ASF_CODEC_ID_G726:      G726　音频编码
 *
************************************************/

typedef enum ASF_CodecID_E 
{
    ASF_CODEC_ID_H264 = 0, 
    ASF_CODEC_ID_G726,
    ASF_CODEC_ID_BUTT
}ASF_CODECID_E;


/************************************************
 * 音频码流信息描述
 *
 * eCodeid:	            [in]	音频编码格式       
 *
 * u32Channels:         [in]    音频通道数
 *
 * u32SamplesPerSec:    [in]    音频采样率
 *
 *u32AvgBytesPerSec:    [in]    音频平均每秒字节数，等于bps/8 ,对于VBR码流，
 *                              如AAC格式,设置为0
 *
 * u16BlockAlign:		[in]    音频数据块大小,以字节为单位，对于CBR码流
 *                              ㄈ鏕711，G726）一般设置为1,对于VBR码流，
 *                              如AAC格式码流该信息代表每个数据块中包含的样
 *                              点个数，多数为1024
 *
 * u16BitsPerSample:    [in]    音频每个采样点所占的比特数，对于VBR码流，设置为0
 *
************************************************/

typedef struct ASF_WAVEFORMATEX_S
{ 
	ASF_CODECID_E         eCodeid;
	unsigned int		  u32Channels;
	unsigned int		  u32SamplesPerSec; 
	unsigned int		  u32AvgBytesPerSec;
	unsigned short		  u16BlockAlign; 
	unsigned short		  u16BitsPerSample; 
} ASF_WAVEFORMATEX_S; 

typedef ASF_WAVEFORMATEX_S ASF_ASTREAM_S;


/************************************************
 * 视频码流信息描述
 *
 * eCodeid:	        [in]    视频编码格式
 *
 * u32BiWidth:      [in]    视频图象的宽
 *
 * u32BiHeight:     [in]    视频图象的高
 *
 * u32BitCount:		[in]    视频色彩深度，如16位色，24真彩等
 *
 * u32avgBitRate:   [in]    视频的码率
 *
 * u32samplePerSec: [in]    视频的帧率
 *
 * u32IsVBR:        [in]    是否为VBR码流，取值1为VBR，0为CBR（仅作信息记录）
 *
 * u32NTSC:         [in]    视频制式，1为N制，0为P制（仅作信息记录）
************************************************/

typedef struct ASF_BITMAPINFOHEADER_S 
{
	ASF_CODECID_E       eCodeid;
    unsigned int		u32BiWidth;
    unsigned int		u32BiHeight;
    unsigned int        u32BitCount;
    unsigned int        u32avgBitRate;
    unsigned int        u32samplePerSec;
    unsigned int        u32IsVBR ;
    unsigned int        u32NTSC ;
} ASF_BITMAPINFOHEADER_S;

typedef ASF_BITMAPINFOHEADER_S ASF_VSTREAM_S;



typedef struct ASF_BINARYINFOHEADER_S   
{
    unsigned int        u32DataLength;
    unsigned char*        pU8BinaryInfoHeader;
} ASF_BINARYINFOHEADER_S;
/************************************************
 * 二进制流信息描述
 *
 * pAddr:     [in]      描述信息起始地址
 *
 * u32Len:	  [in]	    描述信息的长度  
 *
************************************************/

/*struct binary stream, parse by user*/
typedef struct hiASF_BSTREAM_S
{
    void     *pAddr;
    unsigned int      u32Len;
} ASF_BSTREAM_S;


/************************************************
 * MP4播放时长
 *
 * s32Hours:    [in]     小时数   
 *
 * s32Mins:     [in]     分钟数
 *
 * s32Secs      [in]     秒数
 *
************************************************/
typedef struct hiASF_DURATION_S
{
    int		  s32Hours;
    int		  s32Mins;
    int		  s32Secs;
} ASF_DURATION_S;

/************************************************
 * MP4播放时长
 *
 * year:    [in]     2000年开始的年数
 *
 * month:     [in]     月
 *
 * mday      [in]     日
 * 
 * hour:    [in]     小时数   
 *
 * min:     [in]     分钟数
 *
 * sec      [in]     秒数
 *
************************************************/
typedef struct hiASF_DATETIME_S
{
    char		  year;
    char		  month;
    char		  mday;
    char 		  hour;
    char		  min;
    char		  sec;
} ASF_DATETIME_S;

typedef enum hiASF_STREAM_TYPE_E
{
    STREAM_TYPE_VIDEO = 0,
    STREAM_TYPE_AUDIO ,        
    STREAM_TYPE_BINARY , 
    STREAM_TYPE_BUTT 
} ASF_STREAM_TYPE_E;

typedef struct hiASF_STREAM_S
{
    /*stream type: support video, audio, binray type, 
      reference to ASF_STREAM_TYPE_E*/
	unsigned int		  u32Type;		  
    /*video stream struct*/
    ASF_VSTREAM_S struVStream;      
    /*audio stream struct*/
    ASF_ASTREAM_S struAStream;
    /*binary stream struct*/
	ASF_BSTREAM_S struBStream;
} ASF_STREAM_S;


/************************************************
 * asf文件流信息
 *
 * u8FormatName:    [in]    文件格式，如.asf
 *
 * u8FileName:      [in]    文件名称
 *
 * struDuration:    [in]    文件持续时间
 *
 * u32StreamNum:    [in]    文件包含的流数量（复合流、单音频流、单视频流）
 *
 * s32VideoIndex:   [in]    视频流索引号
 *
 * s32AudioIndex:   [in]    音频流索引号
 *
 * s32BinaryIndex:  [in]    二进制流索引号
 *
 * struBitMapInfo:  [in]    视频流信息
 *
 * struWavInfo:     [in]    音频流信息
 *
 * struBStream:     [in]    二进制流信息
 *
************************************************/
typedef struct hiASF_INFO_S
{
    char			u8FormatName[FORMAT_NAME_LEN];
    char			u8FileName[MAX_FILE_NAME];
    ASF_DURATION_S  struDuration;
    unsigned int			u32StreamNum;
    int          s32VideoIndex;
    int          s32AudioIndex;
    int          s32BinaryIndex;

    /*audio stream struct, only s32VideoIndex valid*/
    ASF_VSTREAM_S   struVStream;      
    /*audio stream struct,  ...*/
    ASF_ASTREAM_S   struAStream;
    /*binary stream struct, ...*/
	ASF_BSTREAM_S   struBStream;

    //文件创建日期
    ASF_DATETIME_S struDateTime;

} ASF_INFO_S;


/************************************************
 * ASF文件帧数据信息
 *
 * u64Pts:        [in]    时间戳
 *
 * u64Dts:        [in]    解码时间
 *
 * pU8Data:       [in]    帧数据首地址
 *
 * u32Size:       [in]    帧数据长度
 *
 * u32Type:       [in]    音频帧数据还是视频帧数据
 *
 * u32KeyFlags:   [in]    是否为关键帧
 *
 * u32Duration:   [in]    播放时长
 *
************************************************/
typedef struct hiASF_FRAME_S
{
    ASF_U_64   u64Pts;        /*presentation time stamp in time_base units*/
    ASF_U_64   u64Dts;        /*decompression time stamp in time_base units*/
    unsigned char    *pU8Data;      /*frame addr(or binary addr)*/
    unsigned int   u32Size;       /*frame length*/
    unsigned int   u32Type;       /*index video, audio or binary*/
    unsigned int   u32KeyFlags;   /*key flag, eg I frame*/
    unsigned int   u32Duration;   /* presentation duration in time_base units (0 if not available)*/
} ASF_FRAME_S;

//asf lib init
int ASF_LibInit();

/************************************************************
*函数功能:初始化回调函数，主要用于时间信息获取
*函数参数:GetTime--获取时间回调函数
*返  回   值: 无
*创建作者: chen shi bin
*创建日期:2011.1.6
*************************************************************/
void InitCallbackFunc(AsfDateTimeCallBack GetTime);

/* follow are asf dec interface  */
int  ASF_CreateReader ( ASFHandle* ppAsfHandle, char *pU8FileName );

void ASF_DestroyReader( ASFHandle pAsfHandle ); 

int  ASF_ReadFileInfo( ASFHandle pAsfHandle, ASF_INFO_S *pAsfInfo );

int  ASF_SelectStream(ASFHandle pAsfHandle, unsigned int U32StreamIndex);

int  ASF_ReadFrame( ASFHandle pAsfHandle, ASF_FRAME_S *pFrame );

void ASF_FreeFrame( ASF_FRAME_S *pFrame ); 

int  ASF_SeekFrame(ASFHandle  pAsfHandle, 
                         int		S32StreamIndex, 
                         ASF_64		s64Ts, 
                         int		s32Flag);

/*****************************************************************************
函数功能:把asf 文件定位到文件尾
输入参数:AVFormatContext: 音视频文件上下文
输出参数:无
返  回   值:成功返回0，否则返回-1, 
使用说明:
创建作者:chen shi bin
创建日期:2009/10/15
******************************************************************************/
int ASF_SeekEnd(ASFHandle pAsfHandle);

/*****************************************************************************
函数功能:从asf文件中向前倒退获取一帧视频和一帧二进制流
输入参数:pAsfHandle: asf文件上下文
			  pFrame: 返回视频帧指针
			  bBinaryFrame: 0--不要二进制帧，1--要二进制帧
			  pBFrame: 返回二进制帧的指针
输出参数:无
返  回   值:成功返回0，否则返回-1, 
使用说明:
创建作者:chen shi bin
创建日期:2009/10/15
******************************************************************************/
int ASF_ForwardIFrameEx( ASFHandle pAsfHandle, ASF_FRAME_S *pFrame, int bBinaryFrame , ASF_FRAME_S *pBFrame);

int ASF_BackwardIFrame(ASFHandle pAsfHandle,ASF_FRAME_S *pFrame);

int ASF_ForwardIFrame( ASFHandle pAsfHandle, ASF_FRAME_S *pFrame );


/* follow are asf enc interface */
int ASF_CreateWriter(ASFHandle* ppAsfHandle,unsigned char* pU8FileName);

int ASF_DestroyWriter(ASFHandle  pAsfHandle);

int ASF_SetVideoStreamParams(ASFHandle  pAsfHandle, 
								   int*    pS32StreamNumber, 
								   ASF_BITMAPINFOHEADER_S *PbitMapInfo);

int ASF_SetAudioStreamParams(ASFHandle  pAsfHandle, 
								   int*    pS32StreamNumber,
								   ASF_WAVEFORMATEX_S *pWavInfo);

int ASF_SetBinaryStreamParams(ASFHandle pAsfHandle, 
									int*   pS32StreamNumber, 
									void*  pVBinaryParms, 
									int    s32ParmsLength);

int ASF_BeginWriting(ASFHandle pAsfHandle);
 
int ASF_WriteFrame(ASFHandle  pAsfHandle,
						 int		s32StreamNumber,
						 ASF_64		s64TimeStamp ,
						 unsigned char*		pU8DataBuffer,
						 int		s32DataLength, 
						 int		s32KeyFrameFlag);
 
int ASF_EndWriting(ASFHandle pASFHandle);
int	ASF_Fd(ASFHandle pASFHandle);




#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */


#endif /* End of #ifndef __ASF_INTF_H__ */
